package com.atguigu.gmall.payment.service.impl;

import com.atguigu.gmall.enums.model.PaymentType;
import com.atguigu.gmall.order.model.OrderInfo;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.model.PaymentInfo;
import com.atguigu.gmall.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * title:
 * author: Gyh
 * date:2023-05-09 20:31
 * Description:
 */
@Component
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    /**
     * 保存本地交易记录
     * @param paymentInfo 支付对象
     * @param paymentType 交易类型
     */
    @Override
    @Transactional
    public void savePaymentInfo(PaymentInfo paymentInfo, String paymentType) {
        //1.查询本地支付记录是否存在
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOrderId, paymentInfo.getOrderId());
        queryWrapper.eq(PaymentInfo::getPaymentType,paymentType);
        PaymentInfo info = this.getOne(queryWrapper);
        if (info != null){
            return;
        }
        //保存本地交易记录
        this.save(paymentInfo);
    }

    /**
     * 根据订单编号+支付方式 查询本地交易记录
     * @param outTradeNo
     * @param paymentType
     * @return
     */
    @Override
    public PaymentInfo getPaymentInfoByOutTradeNo(String outTradeNo, String paymentType) {
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOutTradeNo,outTradeNo );
        queryWrapper.eq(PaymentInfo::getPaymentType, paymentType);
        PaymentInfo paymentInfo = this.getOne(queryWrapper);
        return paymentInfo;
    }

    /**
     * 根据订单编号+支付方式 查询本地交易记录
     * @param orderId
     * @param paymentType
     * @return
     */
    @Override
    public PaymentInfo getPaymentInfoByOrderId(Long orderId, String paymentType) {
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOrderId,orderId);
        queryWrapper.eq(PaymentInfo::getPaymentType, paymentType);
        PaymentInfo paymentInfo = this.getOne(queryWrapper);
        return paymentInfo;
    }
}
